package com.appbee.gdata.picasa.client.activity;

import java.util.List;

import javax.inject.Inject;

import com.appbee.core.client.data.BufferedDataProvider;
import com.appbee.core.client.data.IDataUpdater;
import com.appbee.core.client.data.IFetcher;
import com.appbee.core.client.data.ISortInfo;
import com.appbee.gdata.picasa.shared.PicasaRequestFactory;
import com.appbee.gdata.picasa.shared.SimpleAlbumProxy;
import com.google.gwt.view.client.AsyncDataProvider;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.Range;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.google.web.bindery.requestfactory.shared.Request;

public class PicasaAlbumDataProvider extends
		AsyncDataProvider<SimpleAlbumProxy> implements
		IFetcher<SimpleAlbumProxy>, IDataUpdater<SimpleAlbumProxy>

{

	private final PicasaRequestFactory serviceProvider;
	private final BufferedDataProvider<SimpleAlbumProxy> buffer = new BufferedDataProvider<SimpleAlbumProxy>();

	@Inject
	public PicasaAlbumDataProvider(PicasaRequestFactory serviceProvider)
	{
		super();
		this.serviceProvider = serviceProvider;
		buffer.setFetcher(this);
		buffer.setUpdater(this);
	}

	@Override
	protected void onRangeChanged(HasData<SimpleAlbumProxy> display)
	{
		buffer.onRangeChange(display);

	}

	@Override
	public void fetch(Range range, ISortInfo sortInfo,
			Receiver<List<SimpleAlbumProxy>> receiver)
	{
		Request<List<SimpleAlbumProxy>> request = serviceProvider
				.getPicasaService()
				.findAlbums();
		request.fire(receiver);
	}

}
